package com.zyz.rabbitextensionsdemo.listener;

import com.rabbitmq.client.Channel;
import com.zyz.rabbitextensionsdemo.constants.Constants;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

//@Component
public class DlListener {
    @RabbitListener(queues = Constants.NORMAL_QUEUE)
    public void handMessage(Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            // 消费者逻辑
            System.out.printf("接收到消息：%s, deliveryTag %d \n",
                    new String(message.getBody(), "utf-8"), deliveryTag);
            // 进行业务逻辑处理
            System.out.println("业务逻辑处理...");
            int num = 3 / 0;
            System.out.println("业务处理完成");
            // 肯定确认
            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
            // 否定确认
            channel.basicNack(deliveryTag, false, false); // 设置不让入队，变成死信
        }
    }
    @RabbitListener(queues = Constants.DL_QUEUE)
    public void dlHandMessage(Message message) {
        // 死信消费者逻辑
        System.out.printf("[" + Constants.DL_QUEUE + "] 接收到消息：%s, deliveryTag: %d \n",
                new String(message.getBody()), message.getMessageProperties().getDeliveryTag());
    }
}
